package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.reimburse.dto.ProjectBudgetDto;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.PbdBudgetAdjust1;
import com.open.capacity.reimburse.dao.PbdBudgetAdjust1Dao;
import com.open.capacity.reimburse.service.PbdBudgetAdjust1Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PbdBudgetAdjust1ServiceImpl implements PbdBudgetAdjust1Service {

    @Autowired
    private PbdBudgetAdjust1Dao pbdBudgetAdjust1Dao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveOrUpdate(PbdBudgetAdjust1[] list) {
        if (list == null || list.length == 0) {
            return 0;
        }
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        pbdBudgetAdjust1Dao.deleteByProjectId(list[0].getYear(), list[0].getProjectId());
        for (int i = 0; i < list.length; i++) {
            list[i].setId(UUIDUtils.getOriginalUUIDWithoutBar());
            list[i].setUpdateTime(new Date());
            list[i].setUpdateUserid(loginUser.getId());
            list[i].setUpdateUsername(loginUser.getRealName());
            pbdBudgetAdjust1Dao.save(list[i]);
        }
        return 0;
    }

    /**
     * 添加
     *
     * @param pbdBudgetAdjust1
     */
    public int save(PbdBudgetAdjust1 pbdBudgetAdjust1) {
        pbdBudgetAdjust1.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        pbdBudgetAdjust1.setStatus(1);
        pbdBudgetAdjust1.setUpdateTime(new Date());
        return pbdBudgetAdjust1Dao.save(pbdBudgetAdjust1);
    }

    /**
     * 修改
     *
     * @param pbdBudgetAdjust1
     */
    public int update(PbdBudgetAdjust1 pbdBudgetAdjust1) {
        pbdBudgetAdjust1.setUpdateTime(new Date());
        return pbdBudgetAdjust1Dao.update(pbdBudgetAdjust1);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return pbdBudgetAdjust1Dao.delete(id);
    }

    @Override
    public List<ProjectBudgetDto> getChart(Integer year) {
        return pbdBudgetAdjust1Dao.findChart(year);
    }

    /**
     * 查询记录
     *
     * @return
     */
    public List<PbdBudgetAdjust1> findByProjectId(Integer year, String projectId) {
        return pbdBudgetAdjust1Dao.findByProjectId(year, projectId);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<PbdBudgetAdjust1> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<PbdBudgetAdjust1> list = pbdBudgetAdjust1Dao.findAll(params);
        PageInfo<PbdBudgetAdjust1> pageInfo = new PageInfo(list);

        return PageResult.<PbdBudgetAdjust1>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<PbdBudgetAdjust1> findAll(Map<String, Object> params) {
        return pbdBudgetAdjust1Dao.findAll(params);
    }

}
